package org.brisling.mobile.service;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.StringUtil;
import org.brisling.medical.domain.MedicalInpatient;
import org.brisling.mobile.config.MDomain;
import org.brisling.mobile.config.RSAConfig;
import org.brisling.mobile.service.base.AbstractBaseMobileService;
import org.brisling.mobile.vo.sysman.VODept;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.brisling.sysman.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <p>
 * Title: MedicalService
 * </p>
 * <p>
 * Description: 护理风险服务类
 * </p>
 * <p>
 * Company: 长沙东勤信息科技发展有限公司
 * </p>
 * 
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年5月13日 下午4:06:19
 */
@RestController
@RequestMapping("/app/nurse")
public class NurseService extends AbstractBaseMobileService {

	private String queryModule = ConstSysParam.QUERY_MEDICAL_ALL;
	@Autowired
	private UploadService uploadService;

	@Autowired
	private QueryDictionaryService dicService;

	/**
	 * @param path
	 *            路径
	 * @param keycode
	 *            密钥对键值代码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getKey.m")
	public MDomain getKey(HttpServletResponse response) {
		MDomain m = new MDomain();

		String code = RSAConfig.getInitKey();
		m.setC(code);
		Map<PublicKey, PrivateKey> keyMap = RSAConfig.getVerifyKeyPaire(code);

		PublicKey key = RSAConfig.getPublicKey(keyMap);
		byte[] pb = key.getEncoded();
		String pbStr = new String(Base64.encode(pb));
		m.setP(pbStr);

		response.setHeader("Access-Control-Allow-Origin", "*");
		return m;
	}

	/**
	 * 获取版本文件
	 * 
	 * @param id
	 *            版本文件记录id
	 * @return
	 */
	@RequestMapping("/df.m")
	public void downloadFile(@RequestParam(value = "id", required = false) Long id, HttpServletRequest request,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		uploadService.downloadFile(id, request, response);

	}

	@RequestMapping("/getCriticalPatient.m")
	public List<MedicalInpatient> getCriticalPatient(@RequestParam(value = "userid", required = false) String userid,
			@RequestParam(value = "deptid", required = false) String deptid, HttpServletResponse response) {

		response.setHeader("Access-Control-Allow-Origin", "*");

		String sqlid = "qry_current_critically_patient";
		return getPatients(userid, deptid, sqlid);
	}

	/**
	 * 获取当前用户管理的科室
	 * 
	 * @param userid
	 * @param bizcode
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getCriticalDept.m")
	public List<VODept> getCurrentDept(@RequestParam(value = "userid", required = false) String userid,
			@RequestParam(value = "bizcode", required = false) String bizcode, HttpServletRequest request,
			HttpServletResponse response) {

		response.setHeader("Access-Control-Allow-Origin", "*");
		String sqlid = "qry_current_critically_dept";
		return getDept(userid, bizcode, sqlid);
	}

	/**
	 * 获取诊疗部门信息
	 * 
	 * @param userid
	 *            用户id
	 * @param sqlid
	 *            查询sqlid
	 * @return
	 */
	public List<VODept> getDept(String userid, String bizcode, String sqlid) {

		if (userid != null && sqlid != null) {
			String sql = dicService.getSqlByBizcode(sqlid);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("bizcode", bizcode);
			String superUserIdentify = SysConfig.getConfig(ConstSysParam.SUPER_USER_IDENTIFY);
			if (!(superUserIdentify.compareTo(userid) == 0)) {
				if (!this.isSuperDept(userid, ConstSysParam.QUERY_ALL, queryModule)) {
					String deptids = this.getJobDeptids(userid);
					map.put("deptid", deptids);
				}
			}
			sql = StringUtil.formatSql(sql, map);

			List<VODept> lists = null;
			try {
				lists = super.getClassedAllList(sql, VODept.class);
			} catch (DangerousOperatorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return lists;
		}
		return null;

	}

	/**
	 * 查询患者集合
	 * 
	 * @param userid
	 *            用户id
	 * @param deptid
	 *            部门id
	 * @param sqlid
	 *            sqlid
	 * @return
	 */

	public List<MedicalInpatient> getPatients(String userid, String deptid, String sqlid) {

		List<MedicalInpatient> lists = new ArrayList<MedicalInpatient>();

		if (userid != null && deptid != null && sqlid != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userid", userid);
			map.put("deptid", deptid);

			if (!this.isSuperDept(userid, ConstSysParam.QUERY_ALL, queryModule)) {
				String deptids = this.getJobDeptids(userid);
				String _filedName = "deptid";
				map.put(_filedName, deptids);
			}

			String sql = dicService.getSqlByBizcode("qry_current_patient");

			sql = StringUtil.formatSql(sql, map);

			try {
				lists = super.getClassedAllList(sql, MedicalInpatient.class);
			} catch (DangerousOperatorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lists;

	}

}
